<template>
  <div class="login-container">
    <div class="login-box">
      <div style="width: 250px; background-color: #6a707e">
        <div style="display:flex; align-items:center; justify-content:center; padding:20px; margin-bottom:10px;">
          <img style="width: 40px" src="@/assets/imgs/logo.png" alt="房产报告">
          <span style="margin-left:5px; font-size:30px; color:#fff;">房产报告</span>
        </div>
        <div style="line-height:30px;color:#fff;margin-bottom:20px;text-align:center;font-size:20px;">
          <div>海量资源</div>
          <div>您的房产好顾问</div>
        </div>
        <img style="display: block; width: 100%" src="@/assets/imgs/bg_1.jpg" alt="">
      </div>

      <div style="flex: 1; display: flex; justify-content: center; align-items: center">
        <el-form ref="formRef" :model="data.form" :rules="data.rules" style="width: 75%">
          <div style="text-align: center; font-size: 25px; font-weight: bold; margin-bottom: 30px">登 录</div>
          <el-form-item prop="username">
            <el-input size="large" :prefix-icon="User" v-model="data.form.username" placeholder="手机号/邮箱"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password size="large" :prefix-icon="Lock" v-model="data.form.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <div style="margin-bottom: 5px">
            <el-button @click="login" size="large" type="primary" style="width: 100%; background-color: rgb(106, 112, 126); border-color: rgb(106, 112, 126);">登 录</el-button>
          </div>
          <div style="text-align: right">
            还没有账号？请 <a style="color: #1f7aff" href="/register">注册</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const data = reactive({
  dialogVisible: true,
  form: { role: 'USER' },
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' }
    ]
  }
})

const formRef = ref()

const login = () => {
  formRef.value.validate(valid => {
    if (valid) { // 表示表单校验通过
      request.post('/login', data.form).then(resp => {
        if (resp.code === '200') {
          ElMessage.success(resp.msg)
          // 存储用户信息到浏览器的缓存
          localStorage.setItem('xm-user', JSON.stringify(resp.data))
          router.push('/manager/home')
        } else {
          ElMessage.error(resp.msg)
        }
      })
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("@/assets/imgs/bg.jpg");
  background-size: cover;
}
.login-box {
  width: 600px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 10px #0000001a;
  background-color: #fff;
  display: flex;
}
</style>